/************************************************************************
  			Modele/Buffer.java - Copyright ka791748

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/usr/share/apps/umbrello/headings/heading.java

-->Code Generators searches for heading files based on the file extension
   i.e. it will look for a file name ending in ".h" to include in C++ header
   files, and for a file name ending in ".java" to include in all generated
   java code.
   If you name the file "heading.<extension>", Code Generator will always
   choose this file even if there are other files with the same extension in the
   directory. If you name the file something else, it must be the only one with that
   extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on jeu mar 31 2011 at 14:39:42
The original location of this file is /home/ka791748/INF5153/INF5153_TP2_PLAN_B/Modele/Buffer.java
**************************************************************************/

package INF5153_TP3.Modele;

import java.util.Vector;
import java.util.Observable;

public class Buffer extends Observable implements Modele_Buffer {

  private StringBuffer texte;
  private String presse_papier;
  private int position_debut;
  private int position_fin;
  private Vector dernieresCommandes;
  private String message_a_envoyer;
  
  public Buffer () {
      // Init
      texte = new StringBuffer("\n");
      presse_papier="";
      position_debut=0;
      position_fin=0;
      message_a_envoyer="";
      dernieresCommandes = new Vector();
      saveToMemento(); // On enregistre le vide
  }
  /**
   * Set the value of texte
   * @param newVar the new value of texte
   */
  private void setTexte ( String newVar ) {
    texte.setLength(0);
    texte.append(newVar);
    if (texte.charAt(texte.length()-1)!='\n') texte.append("\n");
  }

  /**
   * Get the value of texte
   * @return the value of texte
   */
  private String getTexte ( ) { 
    return texte.toString();
  }

  /**
   * Set the value of presse_papier
   * @param newVar the new value of presse_papier
   */
  private void setPresse_papier ( String newVar ) {
    presse_papier = newVar;
  }

  /**
   * Get the value of presse_papier
   * @return the value of presse_papier
   */
  private String getPresse_papier ( ) {
    return presse_papier;
  }

  /**
   * Set the value of position_debut
   * @param newVar the new value of position_debut
   */
  private void setPosition_debut ( int newVar ) {
    position_debut = newVar;
  }

  /**
   * Get the value of position_debut
   * @return the value of position_debut
   */
  private int getPosition_debut ( ) {
    return position_debut;
  }

  /**
   * Set the value of position_fin
   * @param newVar the new value of position_fin
   */
  private void setPosition_fin ( int newVar ) {
    position_fin = newVar;
  }

  /**
   * Get the value of position_fin
   * @return the value of position_fin
   */
  private int getPosition_fin ( ) {
    return position_fin;
  }

  /**
   * Set the value of dernieresCommandes
   * @param newVar the new value of dernieresCommandes
   */
  private void setDernieresCommandes ( Vector newVar ) {
    dernieresCommandes = newVar;
  }

  /**
   * Get the value of dernieresCommandes
   * @return the value of dernieresCommandes
   */
  private Vector getDernieresCommandes ( ) {
    return dernieresCommandes;
  }




  
  /**
   * ============================================= Début du traitement des commandes
   */
  public void coller( int position )
  {
     if (position<0 ||
         position>texte.length())
     {
         message_a_envoyer = " Coller : sélection impossible ";
     }
     else
     if (presse_papier==null || presse_papier.length()==0)
     {
         message_a_envoyer = " Rien à coller ! ";
     }
     else
     {
         texte.insert(position, presse_papier);
         message_a_envoyer = " Coller complété ";
     }

     avertirObservateur();
  }


  /**
   */
  public void couper(  )
  {
     if (position_debut<0 ||
         position_debut>texte.length() ||
         position_fin<0 ||
         position_fin>texte.length() ||
         position_fin<=position_debut)
     {
         message_a_envoyer = " Couper : sélection impossible ";
     }
     else
     {
         presse_papier = texte.substring(position_debut, position_fin);
         texte.replace(position_debut, position_fin, "");
         position_fin=-1; // pas 2 couper en ligne
         message_a_envoyer = " Couper complété ";
     }

     avertirObservateur();
  }


  /**
   */
  public void copier(  )
  {
     if (position_debut<0 ||
         position_debut>texte.length() ||
         position_fin<0 ||
         position_fin>texte.length() ||
         position_fin<=position_debut)
     {
         message_a_envoyer = " Copier : sélection impossible ";
     }
     else
     {
         presse_papier = texte.substring(position_debut, position_fin);
         position_fin=-1; // pas 2 copier en ligne
         message_a_envoyer = " Copier complété ";
     }

     avertirObservateur();
  }


  /**
   * @param        position
   * @param        texte
   */
  public void inserer( int position, String p_texte )
  {
     if (p_texte==null || p_texte.length()==0)
     {
        message_a_envoyer = " Aucun texte à insérer ? ";
     }
     else
     if (position<0 || position>texte.length())
     {
        message_a_envoyer = " Position invalide ? ";
     }
     else
     {
        message_a_envoyer = " Insérer succès! ";
        texte.insert(position, p_texte);
        this.setTexte(texte.toString());
        position_debut=position_debut+position;
        position_fin=0;
     }

     avertirObservateur();

  }


  /**
   * @param        position_debut
   * @param        position_fin
   */
  public void selectionner( int p_position_debut, int p_position_fin )
  {
     if (p_position_debut<0 ||
         p_position_debut>texte.length() ||
         p_position_fin<0 ||
         p_position_fin>texte.length() ||
         p_position_fin<=p_position_debut)
     {
         message_a_envoyer = " Sélection impossible ";
     }
     else
     {
         position_debut= p_position_debut;
         position_fin = p_position_fin;
         message_a_envoyer = " Sélection complétée ";
     }

     avertirObservateur();
  }


  /**
   */
  public void rejouerCommandes(  )
  {

  }


    /**
     */
    public void refaire() {

        memento m = (memento) careTaker.creer_singleton().empileMemento();
        if (m == null) {
            message_a_envoyer = " Rien à refaire ";
        } else {
            this.setTexte(m.getTexte());
            this.setPosition_debut(m.getPosition_debut());
            this.setPosition_fin(m.getPosition_fin());
            this.setPresse_papier(m.getPresse_papier());

            message_a_envoyer = "Refaire complété";
        }

        avertirObservateur();


    }


    
    /**
     */
    public void defaire() {

        memento m = (memento) careTaker.creer_singleton().depileMemento();
        if (m == null) {
            message_a_envoyer = " Rien à défaire ";
        } else {
            this.setTexte(m.getTexte());
            this.setPosition_debut(m.getPosition_debut());
            this.setPosition_fin(m.getPosition_fin());
            this.setPresse_papier(m.getPresse_papier());

            message_a_envoyer = "Défaire complété";
        }

        avertirObservateur();

    }



  public void saveToMemento( )
  {
    memento m = new memento();
    m.setTexte(this.getTexte());
    m.setPosition_debut(this.getPosition_debut());
    m.setPosition_fin(this.getPosition_fin());
    m.setPresse_papier(this.getPresse_papier());

    careTaker.creer_singleton().addMemento(m);
  }






  public void avertirObservateur()
  {
    // Avertir les observateurs
    Message_Buffer MB= new Message_Buffer();

    MB.setTexte(this.getTexte());
    MB.setMessage(message_a_envoyer);

    setChanged();
    notifyObservers(MB);
  }


}
